// pages/couponReceive/couponReceive.js
const app = getApp()
const mobileReg = /^(13|15|17|18|14|19|16)[0-9]{9}$/
import { requestApi } from '../../api'
import md5 from '../../utils/md5.min.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    channel: '',
    storeIcon: '',
    countryArray: [],
    countryIndex: 0,
    mobilePhone: '',
    couponNo: '',
    vCode: '',
    codeText: '发送验证码',
    codeRequestFlag: false,
    requestFlag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'
    let couponNo = options.couponNo

    this.setData({
      channel: channel,
      couponNo: couponNo
    });
  },

  backHome: function () {
    wx.switchTab({
      url: '../index/index'
    })
  },

  getMobile: function (e) {
    this.setData({
      mobilePhone: e.detail.value
    })
  },

  searchGoods: function (e) {
    this.setData({
      vCode: e.detail.value
    })
  },

  getVcode: function (e) {
    let mobilePhone = this.data.mobilePhone || ''
    let countryArray = this.data.countryArray
    let countryIndex = this.data.countryIndex
    let send_timestamp = new Date().getTime();
    let send_sign = md5('4a68cd1c04adcbd49e599a38f6f82b5a' + send_timestamp + mobilePhone);
    let options = {
      url: requestApi.vCodeUrl,
      method: 'POST',
      data: {
        'send_sign': send_sign,
        'send_timestamp': send_timestamp,
        'receiver': mobilePhone,
        'countrycode': '86',
        'service_type': '2',
        'receiver_type': '1'
      },
    }

    if (mobilePhone == '') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请填写手机号',
      })
      return false
    } else if (mobileReg.test(mobilePhone)) {
      
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请填写正确手机号',
      })
      return false
    }

    if (this.data.codeRequestFlag) return false
    app.request(
      options,
      (data) => {
      },
      (error) => {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '发送失败',
        })
      })

    this.countDown()
  },

  countDown: function () {
    let codeText = ''
    let time = 60

    this.setData({
      codeRequestFlag: true,
    })

    let countDown = setInterval(() => {
      time = time - 1
      codeText = time + '秒后重发'
      this.setData({
        codeText: codeText,
      })
    }, 1000)

    setTimeout(() => {
      clearInterval(countDown)

      this.setData({
        codeRequestFlag: false,
        codeText: '发送验证码',
      })
    }, 60000)
  },

  receiveBtn: function () {
    app.request({
      url: requestApi.receiveCouponUrl,
      method: 'POST',
      data: {
        package_no: this.data.couponNo,
        mobile_phone: this.data.mobilePhone,
        vcode: this.data.vCode,
        account_channel: 0
      }
    }, (data) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: data.original.msg,
      })

      // 领取后将输入框内容清空
      this.setData({
        vCode: '',
        mobilePhone: '',
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })

      // 领取后将输入框内容清空
      this.setData({
        vCode: '',
        mobilePhone: '',
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let couponNo = this.data.couponNo

    return {
      title: '优惠券礼包',
      path: '/pages/couponReceive/couponReceive?couponNo=' + couponNo,
    }
  }
})